<?php
namespace app\wework\home;

use app\common\controller\Wework;
use src\callback\WXBizMsgCrypt;

class ScancodeWaitmsg extends Wework
{

	/**
	 * 扫码验布缩水入口
	 *
	 * @author KING
	 * @date 2020年7月7日
	 *
	 * @param string $body
	 * @param object $wxcpt
	 */
	public function index($body, WXBizMsgCrypt $wxcpt) {
		// 权限检测
		$this->checkRight($wxcpt);

		$qrcode = $body['ScanCodeInfo']['ScanResult'];
		$eventKey = $body['EventKey'];
		$qrcodeArr = explode(',', $qrcode);
		$type = arrayGet($qrcodeArr, '0');
		if (!in_array($type, [1, 2])) {
			$this->replyMessage('未能正确识别二维码');
		}

		if ($type == 1) { // 验布缩水
			if ($eventKey == 'checkcloth') {
				$title = '验布';
			} elseif ($eventKey == 'shrink') {
				$title = '缩水';
			} else {
				$this->replyMessage('未能正确识别验布缩水二维码');
			}

			$clientOrdernum = arrayGet($qrcodeArr, '1');
			$materialId = arrayGet($qrcodeArr, '2');
			$clothNum = arrayGet($qrcodeArr, '3');

			if (empty($clientOrdernum) || empty($materialId) || empty($clothNum)) {
				$this->replyMessage('未能正确识别验布缩水二维码');
			}

			try {
				$url = url('wework/ScancodeWaitmsg/' . $eventKey, ['client_ordernum' => $clientOrdernum, 'material_id' => $materialId, 'cloth_num' => $clothNum], true, true);
				$description = sprintf("<div class=\"highlight\">员工 %s，订单号 %s，材料ID %s，布号 %s</div>", $wxcpt->toUserName, $clientOrdernum, $materialId, $clothNum);
				$btnText = $title . '录入';

				$wework = new \Wework();
				$res = $wework->cardMessageSend($title, $description, $url, $btnText, [$wxcpt->toUserName]);
			} catch (\Exception $e) {
				addLog($e->getMessage(), 'ScancodeWaitmsg-index');
			}

			addLog($res, 'ScancodeWaitmsg-index');

		} elseif ($type == 2) { // 菲码
			$clientOrdernum = arrayGet($qrcodeArr, '1');
			#TODO::

		}
	}

	public function checkcloth() {
		print_r(input('param.'));
		echo "这个是验布录入界面，可自由定义 ";
	}

	public function shrink() {
		print_r(input('param.'));
		echo "这个是缩水录入界面，可自由定义 ";
	}
}